package com.cloud.flowable.controller;

import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.enums.BusinessType;
import com.cloud.flowable.entity.WfCategory;
import com.cloud.flowable.service.IWfCategoryService;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "流程分类")
@RestController
@RequestMapping("/flowable/Category")
public class WfCategoryController extends BaseController {

    @Autowired
    private IWfCategoryService categoryService;

    @Operation(summary = "查询流程分类分页列表")
    @RequestPermissions("flowable:category:list")
    @GetMapping("/list")
    public Result<WfCategory> list(WfCategory wfCategory) {
        startPage();
        List<WfCategory> list = categoryService.getCategoryList(wfCategory);
        return getDataTable(list);
    }

    @Operation(summary = "查询流程分类列表")
    @GetMapping("/allList")
    public Result allList(WfCategory wfCategory) {
        return Result.success("查询成功", categoryService.getCategoryList(wfCategory));
    }

    @Operation(summary = "新增流程分类")
    @RequestPermissions("flowable:category:add")
    @Logs(title = "新增流程分类", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public Result save(@Validated @RequestBody WfCategory category) {
        return toResult(categoryService.saveCategory(category));
    }

    @Operation(summary = "修改流程分类")
    @RequestPermissions("flowable:category:edit")
    @Logs(title = "修改流程分类", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public Result update(@Validated @RequestBody WfCategory wfCategory) {
        return toResult(categoryService.updateCategory(wfCategory));
    }

    @Operation(summary = "流程分类详细信息")
    @RequestPermissions("flowable:category:query")
    @GetMapping("/getInfo")
    public Result getInfo(Long categoryId) {
        return Result.success("查询成功", categoryService.queryById(categoryId));
    }

    @Operation(summary = "删除流程分类")
    @RequestPermissions("flowable:category:remove")
    @Logs(title = "删除流程分类", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Result remove(String categoryIds) {
        List<Long> ids = ToolUtils.setIdsToList(categoryIds);
        return toResult(categoryService.removeIds(ids));
    }

    @Operation(summary = "导出流程分类列表")
    @RequestPermissions("flowable:category:export")
    @Logs(title = "导出流程分类列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(WfCategory wfCategory) {
        categoryService.export(wfCategory);
    }

}
